/*
 * AvailableSeats.fx
 *
 * Created on Apr 27, 2009, 3:28:09 PM
 */

package com.dg.gaming.client.fx.gui;


import com.dg.gaming.api.common.IGame;
import com.dg.gaming.api.common.Player;
import com.dg.gaming.api.common.PlayerList;
import com.dg.gaming.api.common.Table;
import com.dg.gaming.api.common.util.GameProperties;
import com.dg.gaming.client.fx.gui.Seat;
import java.lang.Object;
import java.util.Observable;
import java.util.Observer;
import javafx.ext.swing.SwingLabel;
import javafx.scene.CustomNode;
import javafx.scene.layout.HBox;
import javafx.scene.Node;
import javafx.scene.paint.Color;

/**
 * @author dito
 */

public class AvailableSeats extends CustomNode,Observer{
    
    def WIDTH=20;
    
    var currentPos=0;
    var nodes:Node[];
    var numPlayers = IGame.DEFAULT_NUM_OF_PLAYERS;;
    public var players:PlayerList;
    public var player:Player;
    public var table:Table;
    public var selectedSeat:Integer;

    init{
        nodes=[];
        numPlayers=table.getNumOfPlayers();
        players=table.getPlayerList();
        players.addObserver(this);
           for(i in [0..numPlayers]){
             var node:Node;
                if(i==0)
                {
                  node=SwingLabel {
                    text: "AvailableSeats: "
                }

                }else
                 node=Seat{color:getColor(i),size:WIDTH,select:selectItem,index:i};
                 insert node into nodes;
            }
    }
    public function selectItem(index:Integer):Void{
        if (not player.isSeated()) {
                if (players.isSeatFree(index)) {
                    selectedSeat = index;
                }
                refresh(); // Refresh all components
            }
    }



    function getColor(index:Integer):Color{
        var ret=Color.fromAWTColor( GameProperties.getPlayerColour(index-1));
        
        return ret;
    }

    public override  function create():Node {
        return HBox{
            spacing:1
            content:
            nodes

        }
       ;
    }

    function validateItem(node:Node){
         var seat=node as Seat;
         var index=seat.index;
         seat.color=Color.fromAWTColor(GameProperties.getBackgroundColour());
         var drawBox = players.isSeatFree(index);
         if (not drawBox) {
                drawBox = index.equals(player.getSeatNum());
         }
         if (drawBox) {
             seat.selected=true;
         }

    }

    function refresh(){
           // If a seat isn't selected or a seat isn't free AND a user has sat down yet
        if ((selectedSeat == -1 or not players.isSeatFree(selectedSeat)) and
            player.getSeatNum() == Player.NOT_SEATED) {
            for (i in [0 .. numPlayers-1]) {
                if (players.isSeatFree(i)) {
                    selectedSeat = i;
                    break;
                }
            }
        }

        // Refresh the components
        for (i in [1 .. numPlayers]) {
            validateItem(nodes[i+1]);
        }
    }

    public override function update(arg0:Observable , arg1:Object ) {
      refresh();
    }
}
